package com.chengdu.third.controller.admin;


import com.chengdu.third.pojo.Tsfk;
import com.chengdu.third.pojo.Tsxx;
import com.chengdu.third.service.ITsfkService;
import com.chengdu.third.service.ITsxxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/admin")
public class SuperviseController {

    @Autowired
    private ITsxxService tsxxService;

    @Autowired
    private ITsfkService tsfkService;


    //查询投诉信息的方法
    @GetMapping("/getSupList")
    public String getSupList(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        //规定第几页，每页多少个
//        pagenum是统计页数的
        PageHelper.startPage(pagenum, 5);
        List<Tsxx> list = tsxxService.findAll();
        //得到分页结果对象
        PageInfo<Tsxx> pageInfo = new PageInfo<>(list);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/supervise_list";
    }

    //查询投诉信息的方法
    @GetMapping("/getSupFKList")
    public String getSupFKList(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){

        PageHelper.startPage(pagenum, 5);
        List<Tsfk> tsfks = tsfkService.findAll();
        PageInfo<Tsfk> pageInfo = new PageInfo<>(tsfks);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/superviseFK_list";
    }



    //删除投诉信息
    @GetMapping("/SupDelete/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
       tsxxService.deleteByid(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/getSupList";
    }

    //删除投诉反馈信息
    @GetMapping("/SupFKDelete/{id}/delete")
    public String deleteFK(@PathVariable Long id, RedirectAttributes attributes){
        tsfkService.deleteByid(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/getSupFKList";
    }

    //返回回复人信息
    @GetMapping("/SupAnswer/{id}/answer")
    public String answer(@PathVariable Long id, Model model,RedirectAttributes attributes){
        Tsfk tsfkById = tsfkService.getTsfkById(id);
        if (tsfkById!=null){
            attributes.addFlashAttribute("msg", "已回复");
            return "redirect:/admin/getSupList";
        }
        model.addAttribute("Tsxx", tsxxService.getTsxxById(id));
        return "admin/supervise_answer";
    }

    //提交回复
    @PostMapping("/SupAnswerSubmit/{id}")
    public String SupAnswerSubmit(@PathVariable Long id, Tsfk tsfk, RedirectAttributes attributes){  //修改
        tsfkService.addtsfk(tsfk);
        attributes.addFlashAttribute("msg", "已回复成功");
        return "redirect:/admin/getSupList";
    }



}
